home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12564 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  65 lines

  1. Newsgroups: comp.lang.c,comp.os.ms-windows.programmer.win32
  2. Path: news.inap.net!news1!ind-004-236-183
  3. From: dlmiller@iquest.net (Doug Miller)
  4. Subject: Re: How can I check whether I file exists in a multi-user environment?
  5. X-Nntp-Posting-Host: ind-004-236-183.iquest.net
  6. Message-ID: <Dp6sqL.Buz@iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Network Services
  9. X-Newsreader: News Xpress Version 1.0 Beta #2.1
  10. References: <4jh4tl$t4c_002@chem.uva.nl> <Dp1oM8.Cns@iquest.net> <4jjn1d$clu@solutions.solon.com>
  11. Date: Mon, 1 Apr 1996 14:13:31 GMT
  12.  
  13. seebs@solutions.solon.com (Peter Seebach) wrote:
  14. >In article <Dp1oM8.Cns@iquest.net>, Doug Miller <dlmiller@iquest.net> wrote:
  15. >>        * * * * C A U T I O N * * * *
  16. >>Although this will work in many implementations, to the best of my knowledge
  17. >>it is *NOT* ANSI C.
  18. >
  19. >Then why post it to comp.lang.c?  Why not just email it to the user, along
  20. >with a pointer to the correct newsgroup?  (Which is even in the newsgroups
  21. >list...)
  22. >
  23. >>#include <io.h>
  24. >>int file_exists (char *filename) {
  25. >>    return (access(filename, 0) == 0);
  26. >>} /* returns -1 if file exists, 0 if it does not */
  27. >
  28. >The reason this is a poor solution is that it doesn't test for existance,
  29. >it tests for accessibility.
  30.  
  31. You should check your facts more carefully.  You evidently are unfamiliar with the behavior of
  32. access( ).  It is defined thusly:
  33.  
  34. #include <io.h>
  35. int access (const char *filename, int amode);
  36.  
  37. permissible values for amode are:
  38.     06    check for read and write permission
  39.     04    check for read permission
  40.     02    check for write permission
  41.     01    execute
  42. >>>>    00    check for existence of file    <<<<<
  43.  
  44. Return value: If the requested access is allowed, access() returns 0; otherwise it returns a value
  45. of -1, and the global variable errno is set to one of the following:
  46.     ENOENT        Path or file name not found
  47.     EACCES        Permission denied
  48.  
  49. As I coded it, this is a test for the existence of the file, not its accessibility.
  50.  
  51.  
  52. In modern environments, such as NT or Unix,
  53. >it is quite possible for a file to exist but be inaccessible.  There is
  54. >no real way in such environments to test for existance; you aren't allowed to
  55. >know whether someone else has a file they don't want you to know about.
  56. >
  57. >Also, on any POSIX-like system, access is in <unistd.h>.  This may be useful
  58. >to someone planning to work with multiple systems, or in any event NT, which
  59. >has some POSIX to it.
  60. >
  61. >-s
  62. >--
  63. >Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  64.  
  65.